package com.beiding.workordersystem.common.beans;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.beiding.workordersystem.main.utils.Json;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MsgSender implements InitializingBean {


    @Value("${msgAccessKeyId}")
    private String msgAccessKeyId;

    @Value("${msgAccessSecret}")
    private String msgAccessSecret;

    @Value("${msgSignName}")
    private String msgSignName;

    private DefaultProfile profile;

    public void send(String phone, String templateCode, Map<String, String> params) {
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "轻松企业管理");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", Json.stringify(params));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        profile = DefaultProfile.getProfile("cn-hangzhou", msgAccessKeyId, msgAccessSecret);
    }

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FvCMR2V6VR4uCuayWx2", "U0vxG0YdlrR1wQLrpwSvp15kSYFgRQ");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "15615634868");
        request.putQueryParameter("SignName", "轻松企业管理");
        request.putQueryParameter("TemplateCode", "SMS_184110664");

        Map<String, String> params = new HashMap<>();
        params.put("name", "小满妮儿");
        params.put("orderId", "123");
        params.put("processNumber", "111");
        params.put("processDetails", "测试的工单详情");
        params.put("url", "http://abc.ddd  ");

        request.putQueryParameter("TemplateParam", Json.stringify(params));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
